﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Iacym.Entities;

namespace Iacym.Repository
{
    public class MinisterioRepository : MasterRepository, IMinisterioRepository
    {
        public List<MINISTERIO> GetAllFromMinisterio()
        {
            var query = from m in context.MINISTERIOs
                        select m;
            return query.ToList();
        }

        public List<MINISTERIO> GetFromMinisterioByCriterio(string criterio)
        {
            var query = from p in context.MINISTERIOs
                        select p;

            if (criterio != string.Empty && criterio != null)
            {
                            query = from p in query
                            where p.Descripcion.ToUpper().Contains(criterio.ToUpper())
                            select p;
            }        
            
            return query.ToList();
        }

        public MINISTERIO GetFromMinisterioById(int id)
        {
            return context.MINISTERIOs.Find(id);
        }

        public void AddMinisterio(MINISTERIO ministerio)
        {
            context.MINISTERIOs.Add(ministerio);
            context.SaveChanges();
        }

        public void UpdateMinisterio(MINISTERIO ministerio)
        {
            context.MINISTERIOs.Attach(ministerio);
            context.Entry(ministerio).State = System.Data.EntityState.Modified;
            context.SaveChanges();
        }

        public void RemoveMinisterio(int id)
        {
            var existe = context.MINISTERIOs.Find(id);
            if (existe!=null)
            {
                context.MINISTERIOs.Remove(existe);
                context.SaveChanges();
            }
        }


        public bool ExisteDuplicidadMinisterio(string descripcion, int id)
        {
            var query = from c in context.MINISTERIOs
                        where c.Descripcion.ToUpper().CompareTo(descripcion.ToUpper()) == 0 
                        && c.Id != id
                        select c;

            return query.Any();
        }

    }
}
